
package Control;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import Chat.mysqlConnection;
import Entity.MsgPackage;
import GUI.ScreenManager;
import GUI.ViewRequest_GUI;

public class viewRequest_control {
	
private ViewRequest_GUI view_req_gui;
private MsgPackage ViewRequest_msg;
private String[] result;


public viewRequest_control(){
	view_req_gui = new ViewRequest_GUI();
	ViewRequest_msg = new  MsgPackage();
	
}

public String[] view_request(String req_num){
	ViewRequest_msg.setOp_code(OP_CODE.VIEW_REQ);
	Integer req_n=Integer.parseInt(req_num);
	ViewRequest_msg.setObject(req_n);
	ScreenManager.getConnection().accept(ViewRequest_msg);
	
	//while(ScreenManager.getConnection().getAnswerReceivedFlag() == false);	//wait until flag is changed and shows that result was received from server
	Object o = ScreenManager.getConnection().cclient.getMessage();

	//Object o = ScreenManager.getConnection().getResultobject();
    result=(String[]) o;
    return result;
	

}
public ArrayList<String> get_requests(){
	 ArrayList<String> view;
	int role_num = ScreenManager.getUserName().getRole().intValue();
	if(role_num<5){
	  view = ScreenManager.getUserName().getReq_list();
	 
	}
	else {
		HashMap<Integer, ArrayList<Integer>> map=ScreenManager.getUserName().getReq_auto_list();
		view=new ArrayList<String>();
		for (Integer key: map.keySet()) {

		   ArrayList<Integer> i=map.get(key);
		   Iterator<Integer> it=i.iterator();
		   while(it.hasNext()){
			   String req=it.next().toString();
			   if(!(view.contains(req))){
				   view.add(req);}}}
	}
			   
			   
		   return view;}

public ViewRequest_GUI getView_req_gui() {
	return view_req_gui;
}

public void setView_req_gui(ViewRequest_GUI view_req_gui) {
	this.view_req_gui = view_req_gui;
}

public MsgPackage getViewRequest_msg() {
	return ViewRequest_msg;
}

public void setViewRequest_msg(MsgPackage viewRequest_msg) {
	ViewRequest_msg = viewRequest_msg;
}




}
